﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Asterisk.NET.Manager;
using Asterisk.NET.Manager.Action;
using Asterisk.NET.Manager.Response;
using Asterisk.NET.FastAGI;
using Asterisk.NET.Manager.Event;

namespace PRT
{
    public class trixbox
    {
        const string ASTERISK_HOST = "s131559x.trixbox.fonality.com";
        const int ASTERISK_PORT = 5038;
        const string ASTERISK_LOGINNAME = "remote_mgr";
        const string ASTERISK_LOGINPWD = "0chanc3yo";
        const int TIMEOUT = 10000;

        private static ManagerConnection manager;

        public trixbox()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public string getCallCRMURL()
        {
            return "";

        }

        public int OutboundCall(string channel, string number, int clientID)
        {

            try
            {
                manager = new ManagerConnection(ASTERISK_HOST, ASTERISK_PORT, ASTERISK_LOGINNAME, ASTERISK_LOGINPWD);
                manager.Login();
                OriginateAction action = new OriginateAction();
                action.Context = "internal";
                action.Priority = 1;
                action.Channel = channel;
                action.CallerId = "9793613300";
                action.Exten = number;
                action.Timeout = TIMEOUT;
                ManagerResponse response = manager.SendAction(action);
                AnsweringServiceDataContext db = new AnsweringServiceDataContext();
                
            }
            catch (Exception) { }
                
            return 1;
        }
        /*
        static void dam_UserEvents(object sender, UserEvent e)
        {
            if (e is UserAgentLoginEvent)
            {
                Console.WriteLine("User Event - AgentLogin:"
                    + "\n\tAgent\t\t" + ((UserAgentLoginEvent)e).Agent
                    );
            }
            else
            {
                Console.WriteLine("User Event:"
                    + "\n\tUserEventName\t\t" + e.UserEventName
                    );
                foreach (System.Collections.Generic.KeyValuePair<string, string> pair in e.Attributes)
                {
                    Console.WriteLine(String.Format("\t{0}\t{1}", pair.Key, pair.Value));
                }
            }
        }

        static void manager_IgnoreEvent(object sender, NewExtenEvent e)
        {
            // Ignore this event.
        }
        protected void dam_Events(object sender, ManagerEvent e)
        {
            if (ViewState["CID"] == null || ViewState["DID"] == null)
            {
                StatusEvent statusEvent = e as StatusEvent;
                if (statusEvent != null)
                {
                    if (statusEvent.Channel != null && statusEvent.Channel == channel)
                    {
                        ViewState["DID"] = statusEvent.CallerIdName.Replace("-", "");
                        ViewState["CID"] = statusEvent.CallerId.Replace("-", "");
                    }
                }
            }
        }
        */
    }

    public class UserAgentLoginEvent : UserEvent
    {
        private string agent;
        public string Agent
        {
            get { return agent; }
            set { agent = value; }
        }

        public UserAgentLoginEvent(ManagerConnection source)
            : base(source)
        {
        }
    }

    public struct IncomingCallInfo
    {
        public string CID, DID, uniqueID;
        public IncomingCallInfo(string _CID, string _DID, string _uniqueID)
        {
            CID = _CID;
            DID = _DID;
            uniqueID = _uniqueID;
        }
    }

    public class Channel
    {
        public string Name;
        public string Context;
        public string Exten;
        public string Priority;
        public string Stats;
        public string Application;
        public string Data;
        public string CallerID;
        public string Accountcode;
        public string Amaflags;
        public string Duration;
        public string Bridged;

        public Channel(string showChannelAMIResponse)
        {
            string[] fields = showChannelAMIResponse.Split(':');
            Name = fields[0];
            Context = fields[1];
            Exten = fields[2];
            Priority = fields[3];
            Stats = fields[4];
            Application = fields[5];
            Data = fields[6];
            CallerID = fields[7];
            Accountcode = fields[8];
            Amaflags = fields[9];
            Duration = fields[10];
            Bridged = fields[11];
        }
    }
}
